﻿using BTL_Cloud.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading.Tasks;
using TL_Cloud.Core.Entities;
using TL_Cloud.Core.Services;
using TL_Cloud.DbRepositoryInterface.BookShop;
using TL_Cloud.Models.BookShop;
using TL_Cloud.RepositoryInterface.BookShop;
using TL_Cloud.ViewModels.BookShop;
using TL_Cloud.ViewModels.BookShop.BaseTeacher;

namespace TL_Cloud.RepositoryService.BookShop
{
    public class BaseTeacherRepository : BaseRepository, IBaseTeacherRepository
    {
        public BaseTeacherRepository(AppBookShopContext myContext,
            IPropertyMappingContainer propertyMappingContainer) : base(myContext, propertyMappingContainer)
        {
        }

        public async Task<PaginatedList<Base_TeacherInfo>> GetAllEntitiesAsync(BaseTeacherQueryParameters parameters)
        {
            var query = _myContext.BaseTeacherInfo.AsQueryable(); 
            
            if(!string.IsNullOrEmpty(parameters.keyword))
            {
                string keyword = parameters.keyword.ToLowerInvariant();
                query.Where(n => n.TeacherName.ToLowerInvariant().Contains(keyword));
            }
            
            query = query.ApplySort(parameters.OrderBy, _propertyMappingContainer.Resolve<BaseTeacherResource, Base_TeacherInfo>());

            var count = await query.CountAsync();
            var data = await query
                .Skip(parameters.PageIndex * parameters.PageSize)
                .Take(parameters.PageSize)
                .ToListAsync();

            return new PaginatedList<Base_TeacherInfo>(parameters.PageIndex, parameters.PageSize, count, data);
        }

        public async Task<Base_TeacherInfo> GetEntityByIdAsync(string id)
        {
            return await _myContext.BaseTeacherInfo.FindAsync(id);
        } 

        public void Add(Base_TeacherInfo entity)
        {
            _myContext.BaseTeacherInfo.Add(entity);
        }

        public void Delete(Base_TeacherInfo entity)
        {
            _myContext.BaseTeacherInfo.Remove(entity);
        }

        public void Update(Base_TeacherInfo entity)
        {
            _myContext.Entry(entity).State = EntityState.Modified;
        }

       
    }

 

}
